package cn.zeal4j.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

/**
 * @author Administrator
 * @file IntelliJ IDEA Hibernate-Tutorial
 * @create 2020 09 26 18:43
 */
public class HibernateUtil {

    static {
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().
                configure("hibernate/hibernate.cfg.xml").build();
        sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
    }

    private HibernateUtil() {}

    private static final SessionFactory sessionFactory;

    public static Session getSession() {
        return sessionFactory.openSession();
    }

    public static void destroyFactory() {
        sessionFactory.close();
    }

    public static Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
}
